﻿using System;
using System.Web.UI;
using System.Xml;
using Resources;

namespace ud_GoldWeatherEtc
{
    public partial class RateCompact : UserControl
    {
        public string ImageSiteRoot { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += WeatherLoad;
        }

        void WeatherLoad(object sender, EventArgs e)
        {
            Page.EnableViewState = true;
            PopulateLabel();
            if (!IsPostBack)
            {
                LoadXML();
            }
        }

        private void PopulateLabel()
        {
            ltrTitle.Text = GoldWeatherEtcResource.ExchangeRateName;
        }

        protected void LoadXML()
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx");
                XmlNodeList elements = xmlDoc.GetElementsByTagName("Exrate");
                string s = "";
                
                foreach (XmlNode node in elements)
                {
                    if (node.Attributes["Buy"].Value != "0")
                    {
                        s = "<tr class=\"content\"><td class=\"name\">" + node.Attributes["CurrencyCode"].Value + "</td><td class=\"right\">" + node.Attributes["Sell"].Value + "</td></tr>" + s;
                    }
                }
                s = "<table class=\"wrapper\">" + s;
                s += "</table>";
                Literal1.Text = s;
                ltrSource.Text = @"(" + GoldWeatherEtcResource.Source + @" <span class='vcbimg'></span><span class='vietcombank'>Vietcombank</span>)";
            }
            catch (Exception)
            {
                Literal1.Text = @"The connection can not connect to vietcombank server.";
            }
        }

    }
}